package com.xm9m.ibczy.dsp.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.xm9m.ibczy.dsp.beans.AndoIntoBean;
import com.xm9m.ibczy.dsp.https.HttpFactory;
import com.xm9m.ibczy.dsp.https.http.BCGetRequest;
import com.xm9m.ibczy.dsp.https.http.BCHttpListener;
import com.xm9m.ibczy.dsp.utils.AntLog;

import java.net.HttpURLConnection;

/**
 * Created by baichuan on 2016/11/29.
 */

public abstract class BaseView extends RelativeLayout {

    private static final int DATA = 0x123, IMAGE_NET = 0x124;
    protected AndoIntoBean bean;

    protected View view;

    protected Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DATA:
                    bean = (AndoIntoBean) msg.obj;
                    if (bean != null)
                        bindView();
                    break;
                case IMAGE_NET:

                    break;
            }
        }
    };


    public BaseView(Context context) {
        super(context);
        initThisView(context, null, 0);
    }

    public BaseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initThisView(context, attrs, 0);
    }

    public BaseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initThisView(context, attrs, defStyleAttr);
    }

    public abstract void initThisView(Context context, AttributeSet attrs, int defStyleAttr);

    public abstract void bindView();

    public void setData(AndoIntoBean bean) {
        Message message = new Message();
        message.what = DATA;
        message.obj = bean;
        hand.sendMessage(message);
    }


    public void setImage(final ImageView images, final String imgUrl) {
        if (images == null || imgUrl == null || "".equals(imgUrl.trim()))
            return;
        // 处理图片乱序
        images.setTag(imgUrl);

        AntLog.i("获取网络图片==" + imgUrl);
        BCGetRequest imgRequest = new BCGetRequest(imgUrl);
        imgRequest.withListener(new BCHttpListener() {
            @Override
            public void onSuccess(int statueCode, byte[] data) {
                AntLog.i("statueCodel=" + statueCode + ", url=" + imgUrl);
                if (statueCode == HttpURLConnection.HTTP_OK) {

                    final Bitmap bit = BitmapFactory.decodeByteArray(data, 0, data.length);
                    AntLog.i("url=" + imgUrl + ",   bitmap is Null=" + (null == bit)+",   data is null="+(data==null));
                    if (bit != null) {
                        AntLog.i("获得网络图片成功");
                        images.post(new Runnable() {
                            @Override
                            public void run() {
                                images.setImageBitmap(bit);
                            }
                        });
                    }else{
                        AntLog.i("获得网络图片失败");
                    }

                }
            }

            @Override
            public void onError(int statueCode, String message) {
                AntLog.i("获得网络图片失败");
            }
        });
        HttpFactory.addRequest(imgRequest);
    }
}
